<template>
	<view class="password-container">
		<view class="form-section">
			<view class="form-item">
				<text class="label">原密码</text>
				<input type="password" v-model="formData.oldPassword" placeholder="请输入原密码" class="input" />
			</view>
			
			<view class="form-item">
				<text class="label">新密码</text>
				<input type="password" v-model="formData.newPassword" placeholder="请输入新密码" class="input" />
			</view>
			
			<view class="form-item">
				<text class="label">确认密码</text>
				<input type="password" v-model="formData.confirmPassword" placeholder="请再次输入新密码" class="input" />
			</view>
		</view>
		
		<button class="submit-btn" @click="handleSubmit">确认修改</button>
	</view>
</template>

<script>
	import { changePassword } from '@/api/user.js'
	
	export default {
		data() {
			return {
				formData: {
					oldPassword: '',
					newPassword: '',
					confirmPassword: ''
				}
			}
		},
		methods: {
			// 验证密码强度
			checkPasswordStrength(password) {
				// 密码长度至少8位，包含数字和字母
				const reg = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/
				return reg.test(password)
			},
			// 提交表单
			async handleSubmit() {
				// 表单验证
				if (!this.formData.oldPassword) {
					uni.showToast({
						title: '请输入原密码',
						icon: 'none'
					})
					return
				}
				if (!this.formData.newPassword) {
					uni.showToast({
						title: '请输入新密码',
						icon: 'none'
					})
					return
				}
				if (!this.formData.confirmPassword) {
					uni.showToast({
						title: '请确认新密码',
						icon: 'none'
					})
					return
				}
				if (this.formData.newPassword !== this.formData.confirmPassword) {
					uni.showToast({
						title: '两次输入的密码不一致',
						icon: 'none'
					})
					return
				}
				if (!this.checkPasswordStrength(this.formData.newPassword)) {
					uni.showToast({
						title: '密码必须包含数字和字母，且长度至少8位',
						icon: 'none'
					})
					return
				}
				
				uni.showLoading({
					title: '修改中...'
				})
				
				try {
					const res = await changePassword({
						oldPassword: this.formData.oldPassword,
						newPassword: this.formData.newPassword
					})
					
					if (res.code === 200) {
						uni.showToast({
							title: '修改成功',
							icon: 'success'
						})
						
						// 返回上一页
						setTimeout(() => {
							uni.navigateBack()
						}, 1500)
					} else {
						uni.showToast({
							title: res.msg || '修改失败',
							icon: 'none'
						})
					}
				} catch (err) {
					console.error('修改失败:', err)
					uni.showToast({
						title: '修改失败',
						icon: 'none'
					})
				} finally {
					uni.hideLoading()
				}
			}
		}
	}
</script>

<style>
	.password-container {
		min-height: 100vh;
		background-color: #f8f8f8;
		padding: 20rpx;
	}
	
	.form-section {
		background-color: #fff;
		padding: 30rpx;
		border-radius: 12rpx;
	}
	
	.form-item {
		margin-bottom: 30rpx;
	}
	
	.form-item:last-child {
		margin-bottom: 0;
	}
	
	.label {
		font-size: 28rpx;
		color: #333;
		margin-bottom: 20rpx;
		display: block;
	}
	
	.input {
		width: 100%;
		height: 80rpx;
		border: 1rpx solid #eee;
		border-radius: 8rpx;
		padding: 0 20rpx;
		font-size: 28rpx;
	}
	
	.submit-btn {
		width: 100%;
		height: 88rpx;
		line-height: 88rpx;
		background-color: #007AFF;
		color: #fff;
		font-size: 32rpx;
		border-radius: 44rpx;
		margin-top: 60rpx;
	}
	
	.submit-btn:active {
		opacity: 0.8;
	}
</style> 